<template>
  <el-container>
    <el-header />
    <el-main>
      <el-col :span="1"><div class="grid-content bg-purple-light">
        <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addDictionaryParamClick">新增</el-button>
      </div></el-col>
      <template>
        <el-table :data="dictionary_params" stripe border style="width: 100%">
          <el-table-column prop="name" label="字典名称" width="180" />
          <el-table-column prop="data" label="字典数据" width="180" />
          <el-table-column prop="label" label="字典标签" width="180" />
          <el-table-column prop="code" label="字典唯一编码" width="180" />
          <el-table-column label="是否启用">
            <template slot-scope="scope">
              {{ scope.row.isuse === '1'?'是':'否' }}
            </template>
          </el-table-column>
          <el-table-column prop="number" label="排序" width="180" />
          <el-table-column
            fixed="right"
            label="操作"
            width="180"
          >
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="editClick(scope.row)">编辑</el-button>
              <el-button type="danger" size="mini" @click="deleteClick(scope.row)">删 除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-main>

    <el-footer>
      <div class="block">
        <el-pagination
          :current-page="page.current"
          :page-sizes="[5,10, 20, 50, 100]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-footer>

    <el-dialog :title="edit?'字典项修改':'字典项添加'" :visible.sync="dialogFormVisible" :close-on-click-modal="false" center append-to-body style="z-index:9999" :inline="true">
      <el-form :model="tempDirectoryParam" label-width="130px" :inline="true">
        <el-form-item label="数据名称">
          <el-input v-model="tempDirectoryParam.name" />
        </el-form-item>
        <el-form-item label="数据">
          <el-input v-model="tempDirectoryParam.data" />
        </el-form-item>
        <el-form-item label="标签">
          <el-input v-model="tempDirectoryParam.label" />
        </el-form-item>
        <el-form-item label="所属目录code">
          <el-input v-model="tempDirectoryParam.code" />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select v-model="tempDirectoryParam.isuse" placeholder="请选择">
            <el-option
              v-for="item in dir_logic"
              :key="item.id"
              :label="item.label"
              :value="item.data"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="tempDirectoryParam.number" />
        </el-form-item>
        <el-form-item label="拓展值">
          <el-input v-model="tempDirectoryParam.extdata1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="!edit" type="primary" @click="addDirectoryParamTask">确 定</el-button>
        <el-button v-if="edit" type="primary" @click="editDirectoryParamTask">修 改</el-button>
      </div>
    </el-dialog>

    <my-delete-dialog
      :show-dialog="dialogConfirmVisible"
      :row="tempDirectoryParam"
      @deleteTask="deleteTask"
      @cancelTask="dialogConfirmVisible = false"
    />

  </el-container>
</template>
<script>
import request from '@/utils/request'
export default {
  props: { 'dictionaryDir': Object },
  data: function() {
    return {
      edit: false,
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      orders: [
        { field: 'number' }
      ],
      tempDirectoryParam: {},
      dialogFormVisible: false,
      dialogConfirmVisible: false,
      dictionary_params: []
    }
  },
  computed: {
    dir_logic: function() {
      return this.$store.state.directory.directory['logic']
    }
  },
  watch: {
    dictionaryDir: function() {
      this.page.current = 1
      this.query()
    },
    'tempDirectoryParam.name': function() {
      this.tempDirectoryParam.label = this.tempDirectoryParam.name
    }
  },
  created() {
    this.directoryUpdate()
    this.query()
  },
  methods: {
    directoryUpdate: function() {
      this.$store.commit('directory/setDirectory', 'logic')
    },
    addDictionaryParamClick: function() {
      this.edit = false
      this.tempDirectoryParam = {}
      this.tempDirectoryParam.code = this.dictionaryDir.code
      this.dialogFormVisible = true
    },
    addDirectoryParamTask: function() {
      this.$progress.start()
      request({
        url: '/preset/dictionary_param/add',
        method: 'post',
        data: this.tempDirectoryParam
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.tempDirectoryParam = {}
          this.dialogFormVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
      ).catch(() => { this.$progress.done() })
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    editClick: function(param) {
      this.tempDirectoryParam = param
      this.dialogFormVisible = true
      this.edit = true
    },
    editDirectoryParamTask: function() {
      this.$progress.start()
      request({
        url: '/preset/dictionary_param/edit',
        method: 'post',
        data: this.tempDirectoryParam
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.tempDirectoryParam = {}
          this.dialogFormVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
      ).catch(() => { this.$progress.done() })
    },
    deleteClick: function(row) {
      this.tempDirectoryParam = row
      this.dialogConfirmVisible = true
    },
    deleteTask: function() {
      this.$progress.start()
      request({
        url: '/preset/dictionary_param/delete',
        method: 'post',
        data: this.tempDirectoryParam
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 字典目录查询
    query: function() {
      const that = this
      const data = {
        current: that.page.current,
        size: that.page.size,
        total: that.page.total,
        conditions: [
          {
            field: 'code',
            symbol: 'EQ',
            type: 'STRING',
            value: that.dictionaryDir.code
          }
        ],
        orders: that.orders
      }
      this.$progress.start()
      request({
        url: '/preset/dictionary_param/query',
        method: 'post',
        data: data
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.dictionary_params = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
